package com.sskj.cndragon.ui.activity;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bulong.rudeness.RudenessScreenHelper;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.cndragon.R;
import com.sskj.cndragon.bean.AddressBean;
import com.sskj.cndragon.bean.AssetBean;
import com.sskj.cndragon.bean.AssetListBean;
import com.sskj.cndragon.bean.RecordListBean;
import com.sskj.cndragon.component.DaggerUserDataComponent;
import com.sskj.cndragon.presenter.AssetActivityPresenter;
import com.sskj.cndragon.view.ToolBarLayout;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.NumberUtil;
import com.sskj.common.util.SPUtil;
import com.sskj.common.util.TimeFormatUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.SPConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.TabEntity;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.mvchelper.ModelRx2DataSource;
import com.sskj.lib.util.HideUtil;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;


public class AssetActivity extends BaseActivity<AssetActivityPresenter> {
    @BindView(R.id.toolbar)
    ToolBarLayout toolbar;
    @BindView(R.id.tv_zhuanzeng)
    TextView tvZhuanzeng;
    @BindView(R.id.tv_huazhuan)
    TextView tvHuazhuan;
    @BindView(R.id.tv_withdraw)
    TextView tvWithdraw;
    @BindView(R.id.tv_recharge)
    TextView tvRecharge;
    @BindView(R.id.tv_money_fenghuang)
    TextView tvMoneyFenghuang;
    @BindView(R.id.tv_money_long)
    TextView tvMoneyLong;
    @BindView(R.id.tv_money_shuijing)
    TextView tvMoneyShuijing;
    @BindView(R.id.tv_money_choujiang)
    TextView tvMoneyChoujiang;
    @BindView(R.id.iv_zhuan)
    ImageView ivZhuan;
    @BindView(R.id.commonTabLayout)
    CommonTabLayout commonTabLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.coolRefreshView)
    CoolRefreshView coolRefreshView;
    @Inject
    UserViewModel userViewModel;

    private UserData userData;

    private String fenghangbi;

    private int type = 2;


    private String[] mTitles = {App.INSTANCE.getString(R.string.shuijing), App.INSTANCE.getString(R.string.longbi), App.INSTANCE.getString(R.string.fenghuangbi)};

    private SlimAdapter<AssetListBean.AssetBean> slimAdapter;
    private MVCCoolHelper<List<AssetListBean.AssetBean>> mvcHelper;

    public static void start(Context context) {
        Intent intent = new Intent(context, AssetActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_asset;
    }

    @Override
    public AssetActivityPresenter getPresenter() {
        return new AssetActivityPresenter();
    }

    @Override
    protected void initView() {
        super.initView();
        DaggerUserDataComponent.create().inject(this);
        toolbar.setLeftButtonOnClickListener(view -> finish());
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
            } else {
                userData = null;
            }
        });

        ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i]));
        }
        commonTabLayout.setTabData(mTabEntities);
        commonTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                type = position + 2;
                if (mvcHelper != null) {
                    mvcHelper.refresh();
                }
            }

            @Override
            public void onTabReselect(int position) {

            }
        });

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(this)
                .setFirstDraw(false)
                .setLastDraw(false)
                .setPaintColor(ContextCompat.getColor(this, R.color.divider_s))
                .setPaintWidth((int) RudenessScreenHelper.pt2px(this, 1))
                .setLeftPadding((int) RudenessScreenHelper.pt2px(this, 30))
                .setRightPadding((int) RudenessScreenHelper.pt2px(this, 30)));
        slimAdapter = SlimAdapter.create()
                .register(R.layout.item_asset, new SlimInjector<AssetListBean.AssetBean>() {
                    @Override
                    public void onInject(AssetListBean.AssetBean data, IViewInjector injector, List payloads) {
                        injector.text(R.id.tv_date, TimeFormatUtil.formatE(data.getCreateTime()))
                                .text(R.id.tv_money, data.getMoney().contains("-") ? data.getMoney() : "+" + data.getMoney())
                                .text(R.id.tv_type, data.getDetail());
                    }
                }).attachTo(recyclerView).updateData(new ArrayList());
        mvcHelper = new MVCCoolHelper<>(coolRefreshView);
        ModelRx2DataSource<AssetListBean.AssetBean> dataSource = new ModelRx2DataSource<>(
                (ModelRx2DataSource.OnLoadSource<AssetListBean.AssetBean>)
                        page -> mPresenter.getAssetRecord(type, page), 10);

        mvcHelper.setDataSource(dataSource);
        mvcHelper.setAdapter(slimAdapter);
        mvcHelper.setAutoLoadMore(true);
        mvcHelper.setNeedCheckNetwork(false);
        mvcHelper.refresh();
    }


    @Override
    protected void onRestart() {
        super.onRestart();
        mPresenter.getAsset(Integer.valueOf(SPUtil.get(SPConfig.ID, "")));
        if (mvcHelper != null) {
            mvcHelper.refresh();
        }
    }

    @Override
    protected void initData() {
        super.initData();
        mPresenter.getAsset(Integer.valueOf(SPUtil.get(SPConfig.ID, "")));
        ClickUtil.click(300, tvRecharge, () -> {
            if (userData == null) {
                return;
            }
            RechargeActivity.start(this);
        });
        ClickUtil.click(300, tvWithdraw, () -> {
            if (userData == null) {
                return;
            }
            if (userData.getChinaPwd() == 0) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.appWeishezhizhifumima));
                return;
            }
            WithDrawActivity.start(this);
        });
        ClickUtil.click(300, tvHuazhuan, () -> {
            if (userData == null) {
                return;
            }
            if (userData.getChinaPwd() == 0) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.appWeishezhizhifumima));
                return;
            }
            if (fenghangbi == null) {
                return;
            }
            TransferActivity.start(this, fenghangbi);
        });
        ClickUtil.click(300, tvZhuanzeng, () -> {
            if (userData == null) {
                return;
            }
            if (userData.getChinaPwd() == 0) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.appWeishezhizhifumima));
                return;
            }
            SendActivity.start(this);
        });
        ClickUtil.click(300, ivZhuan, () -> mPresenter.getAddresses(SPUtil.get(SPConfig.ID, "")));
    }

    public void updataAsset(AssetBean data) {
        if (data == null) {
            return;
        }
        tvMoneyFenghuang.setText(NumberUtil.keep2(data.getUnablePhoenixCoin()));
        tvMoneyLong.setText(NumberUtil.keep2(data.getDragonCoin()));
        tvMoneyShuijing.setText(NumberUtil.keep2(data.getCrystalCoin()));
        tvMoneyChoujiang.setText(NumberUtil.keepNoZero(data.getRaffleTickets()));
        fenghangbi = data.getUnablePhoenixCoin();
    }

    public void getAddressOK(List<AddressBean> data) {
        if (data == null || data.size() == 0) {
            ToastUtil.showShort(App.INSTANCE.getString(R.string.appMeiyoushouhuodizhi));
            return;
        }
        TurntableActivity.start(this);
    }
}
